package com.ls.dao;

import java.util.ArrayList;
import java.util.List;

import com.ls.entity.Dt_Order_Goods;
import com.ls.entity.Dt_Orders;
import com.ls.util.DBHelper;
import com.ls.util.JsonUtil;

public class Dt_OrdersDao {
	public String orderWayJson(int user_id) {
		String sql = "SELECT * FROM view_app_orders where user_id = " + user_id;
		return new DBHelper().executeQueryList(sql);
	}

	public static String insertOrderWayJson(Dt_Orders orders) {
		// INSERT INTO dt_orders
		// (user_id,user_name,mobile,order_amount)VALUES(1,'admin','18678716469',120)
		StringBuffer sql1 = new StringBuffer(
				"INSERT INTO dt_orders (user_id,user_name,mobile,order_amount)");
		sql1.append("VALUES(" + orders.getUser_id() + ",'");
		sql1.append(orders.getUser_name() + "','");
		sql1.append(orders.getMobile() + "',");
		sql1.append(orders.getOrder_amount() + ")");

		// INSERT INTO dt_order_goods
		// (article_id,order_id,goods_title,img_url,goods_price,real_price)VALUES(7,1,'sad','1.png',12,12)
		Dt_Order_Goods order_Goods = orders.getOrder_Goods();

		orders.setOrder_Goods(order_Goods);
		StringBuffer sql2 = new StringBuffer(
				" INSERT INTO dt_order_goods(article_id,order_id,goods_title,img_url,goods_price,real_price)");
		sql2.append("VALUES(" + order_Goods.getArticle_id() + ",");
		sql2.append("?,'");
		sql2.append(order_Goods.getGoods_title() + "','");
		sql2.append(order_Goods.getImg_url() + "',");
		sql2.append(order_Goods.getGoods_price() + ",");
		sql2.append(order_Goods.getDecimal() + ")");
		List<String> lsSql = new ArrayList<String>();

		lsSql.add(sql1.toString());
		lsSql.add(sql2.toString());
		System.out.println(JsonUtil.toJson(orders));
		System.out.println(JsonUtil.toJson(lsSql));
		System.out.println(sql2.toString());
		return new DBHelper().executeInsert(lsSql);
	}

	public static void main(String[] args) {
		Dt_Orders orders = new Dt_Orders();
		orders.setUser_id(1);
		orders.setUser_name("aaa");
		orders.setMobile("18678716469");
		orders.setOrder_amount(120);

		Dt_Order_Goods order_Goods = new Dt_Order_Goods();
		order_Goods.setArticle_id(121);
		order_Goods.setOrder_id(orders.getId());
		order_Goods.setGoods_title("aaa");
		order_Goods.setImg_url("111");
		order_Goods.setGoods_price(122);
		order_Goods.setDecimal(12);
		orders.setOrder_Goods(order_Goods);
		insertOrderWayJson(orders);
	}
}
